﻿using System;
using MetaVideoEditorEvolved.Engine;
using MetaVideoEditorEvolved.Engine.Entity;
using MetaVideoEditorEvolved.Helper;

namespace MetaVideoEditorEvolved.ViewModel
{
	using System.Collections.Generic;
	using System.Collections.ObjectModel;
	using System.ComponentModel;
	using System.Linq;
	using System.Windows;
	using System.Windows.Forms;
	using System.Windows.Input;
	using GalaSoft.MvvmLight;
	using GalaSoft.MvvmLight.Command;
	using GalaSoft.MvvmLight.Messaging;
	using MetaVideoEditorEvolved.Messages;
	using mveEngine;

	public class SettingsWindowVM : ViewModelBase
	{
		private ObservableCollection<string> folders,
		mediaBrowserFolders;

		private ObservableCollection<AddInInformation> saverAddIns,
		providerAddIns,
		readerAddIns;

		private AddInInformation saverAddInSelected,
		providerAddInSelected,
		readerAddInSelected;
		
		private string folderSelected,
		filmExtension,
		backgroundDisplaySelected,
		patternMovies,
		patternSeries,
		patternSeason,
		patternEpisodes,
		vlcPath,
		selectedTraceLevel;

		private bool isActivateDebug,
		isOnlyAddMissingData,
		isSaveWindowGeometry,
		canUseColorsInTheThree,
		enableMoviesRenaming,
		enableSeriesRenaming,
		enableSeasonsRenaming,
		enableEpisodeRenaming,
		removeAccentsFromCharacters,
		useProxy,
		useGenreMapping;

		private int minimumSizeSelected,
		numberBackdropChoiceSelected,
		localPluginSelectedIndex,
		providerPluginSelectedIndex,
		saverPluginSelectedIndex;
		
		private ObservableCollection<string> traceLevels;
		
		private Visibility progressBarVisibility = Visibility.Collapsed;

		public SettingsWindowVM()
		{					
			saverAddIns = new ObservableCollection<AddInInformation>(from a in AddInSaverManager.Current.AddIns
			                                                         orderby a.Index
			                                                         select a);
			
			providerAddIns = new ObservableCollection<AddInInformation>(from a in AddInProviderManager.Current.AddIns
			                                                            orderby a.Index
			                                                            select a);
			
			readerAddIns = new ObservableCollection<AddInInformation>(from a in AddInReaderManager.Current.AddIns
			                                                          orderby a.Index
			                                                          select a);
			
			traceLevels =  new ObservableCollection<string>(TracerLevel.GetNames(typeof(TracerLevel)));
			
			Folders = new ObservableCollection<string>(Config.Instance.RootFolders);
			MediaBrowserFolders = new ObservableCollection<string>(FileUtil.GetMBfolders());
			IsActivateDebug = Config.Instance.EnableTraceLogging;
			IsOnlyAddMissingData = Config.Instance.AddMissingData;
			FilmExtension = Config.Instance.FilmExtension;
			MinimumSizeSelected = Config.Instance.MinBackdropWidth;
			NumberBackdropChoiceSelected = Config.Instance.MaxBdSaved;
			BackgroundDisplaySelected = Config.Instance.Background;
			IsSaveWindowGeometry = Config.Instance.FullscreenMode;
			CanUseColorsInTheThree = Config.Instance.UseTreeColor;
			EnableEpisodeRenaming = Config.Instance.RenameEpisodes;
			EnableMoviesRenaming = Config.Instance.RenameMovies;
			EnableSeasonsRenaming = Config.Instance.RenameSeasons;
			EnableSeriesRenaming = Config.Instance.RenameSeries;
			RemoveAccentsFromCharacters = Config.Instance.RemoveAccents;
			PatternEpisodes = Config.Instance.EpisodePattern;
			PatternMovies = Config.Instance.MoviePattern;
			PatternSeason = Config.Instance.SeasonPattern;
			PatternSeries = Config.Instance.SeriesPattern;
			VlcPath = Config.Instance.VlcPath;
			UseProxy= Config.Instance.useProxy;
			SelectedTraceLevel = Config.Instance.TraceLevel;
			UseGenreMapping = Config.Instance.UseGenreMapping;
		}

		#region TabItem : Folders
		public ObservableCollection<string> Folders
		{
			get
			{
				return folders;
			}

			set
			{
				folders = value;
				RaisePropertyChanged("Folders");
			}
		}

		public ObservableCollection<string> MediaBrowserFolders
		{
			get
			{
				return mediaBrowserFolders;
			}

			set
			{
				mediaBrowserFolders = value;
				RaisePropertyChanged("MediaBrowserFolders");
			}
		}

		public string FolderSelected
		{
			get
			{
				return folderSelected;
			}

			set
			{
				folderSelected = value;
				RaisePropertyChanged("FolderSelected");
			}
		}

		public ICommand SetVlcPathCommand
		{
			get
			{
				return new RelayCommand(()=>
				                        {
				                        	OpenFileDialog dialog = new OpenFileDialog();
				                        	dialog.Filter="vlc application|vlc.exe";
				                        	
				                        	if(dialog.ShowDialog()== DialogResult.OK)
				                        	{
				                        		this.VlcPath = dialog.FileName;
				                        	}
				                        });
			}
		}
		
		public ICommand AddFolderCommand
		{
			get
			{
				return new RelayCommand(() =>
				                        {
				                        	FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
				                        	var result = folderBrowser.ShowDialog();
				                        	if (result == System.Windows.Forms.DialogResult.OK)
				                        	{
				                        		if (!Folders.Contains(folderBrowser.SelectedPath))
				                        		{
				                        			Folders.Add(folderBrowser.SelectedPath);
				                        			CanRebuild = true;
				                        		}
				                        	}
				                        });
			}
		}

		public ICommand RemoveFolderCommand
		{
			get
			{
				return new RelayCommand(() =>
				                        {
				                        	Folders.Remove(FolderSelected);
				                        	CanRebuild = true;
				                        },
				                        () =>
				                        {
				                        	return !string.IsNullOrEmpty(FolderSelected);
				                        });
			}
		}

		public ICommand MediaBrowserFolderCommand
		{
			get
			{
				return new RelayCommand(() =>
				                        {
				                        	foreach (var item in MediaBrowserFolders)
				                        	{
				                        		if (!Folders.Contains(item))
				                        		{
				                        			Folders.Add(item);
				                        			CanRebuild = true;
				                        		}
				                        	}
				                        });
			}
		}
		#endregion

		#region TabItem : General
		
		public ObservableCollection<string> TraceLevels
		{
			get
			{
				return this.traceLevels;
			}
			
			set
			{
				this.traceLevels = value;
				RaisePropertyChanged("TraceLevels");
			}
		}
		
		public string SelectedTraceLevel
		{
			get
			{
				return this.selectedTraceLevel;
			}
			
			set
			{
				this.selectedTraceLevel = value;
				RaisePropertyChanged("SelectedTraceLevel");
			}
		}
		
		public bool UseProxy
		{
			get
			{
				return useProxy;
			}
			
			set
			{
				useProxy = value;
				RaisePropertyChanged("UseProxy");
			}
		}
		
		public bool UseGenreMapping
		{
			get
			{
				return useGenreMapping;
			}
			
			set
			{
				useGenreMapping = value;
				RaisePropertyChanged("UseGenreMapping");
			}
		}
		
		public string VlcPath
		{
			get
			{
				return vlcPath;
			}
			
			set
			{
				vlcPath = value;
				RaisePropertyChanged("VlcPath");
			}
		}
		
		public bool IsActivateDebug
		{
			get
			{
				return isActivateDebug;
			}

			set
			{
				isActivateDebug = value;
				RaisePropertyChanged("IsActivateDebug");
			}
		}

		public bool IsOnlyAddMissingData
		{
			get
			{
				return isOnlyAddMissingData;
			}

			set
			{
				isOnlyAddMissingData = value;
				RaisePropertyChanged("IsOnlyAddMissingData");
			}
		}

		public string FilmExtension
		{
			get
			{
				return filmExtension;
			}

			set
			{
				filmExtension = value;
				RaisePropertyChanged("FilmExtension");
			}
		}

		public List<int> MinimumSizes
		{
			get
			{
				return new List<int> { 0, 200, 400, 600, 800, 1000, 1200, 1600, 2000 };
			}
		}

		public int MinimumSizeSelected
		{
			get
			{
				return minimumSizeSelected;
			}

			set
			{
				minimumSizeSelected = value;
				RaisePropertyChanged("MinimumSizeSelected");
			}
		}

		public List<int> NumberBackdropChoices
		{
			get
			{
				return new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1000 };
			}
		}

		public int NumberBackdropChoiceSelected
		{
			get
			{
				return numberBackdropChoiceSelected;
			}

			set
			{
				numberBackdropChoiceSelected = value;
				RaisePropertyChanged("NumberBackdropChoiceSelected");
			}
		}
		#endregion

		#region TabItem : Display
		public List<string> BackgroundDisplay
		{
			get
			{
				return new List<string>
				{
					"wmc",
					"grey"
				};
			}
		}

		public string BackgroundDisplaySelected
		{
			get
			{
				return backgroundDisplaySelected;
			}

			set
			{
				backgroundDisplaySelected = value;
				RaisePropertyChanged("BackgroundDisplaySelected");
			}
		}

		public bool IsSaveWindowGeometry
		{
			get
			{
				return isSaveWindowGeometry;
			}

			set
			{
				isSaveWindowGeometry = value;
				RaisePropertyChanged("IsSaveWindowGeometry");
			}
		}

		public bool CanUseColorsInTheThree
		{
			get
			{
				return canUseColorsInTheThree;
			}

			set
			{
				canUseColorsInTheThree = value;
				RaisePropertyChanged("CanUseColorsInTheThree");
			}
		}
		#endregion

		#region TabItem : Renaming
		public bool EnableMoviesRenaming
		{
			get
			{
				return enableMoviesRenaming;
			}

			set
			{
				enableMoviesRenaming = value;
				RaisePropertyChanged("EnableMoviesRenaming");
			}
		}

		public bool EnableSeriesRenaming
		{
			get
			{
				return enableSeriesRenaming;
			}

			set
			{
				enableSeriesRenaming = value;
				RaisePropertyChanged("EnableSeriesRenaming");
			}
		}

		public bool EnableSeasonsRenaming
		{
			get
			{
				return enableSeasonsRenaming;
			}

			set
			{
				enableSeasonsRenaming = value;
				RaisePropertyChanged("EnableSeasonsRenaming");
			}
		}

		public bool EnableEpisodeRenaming
		{
			get
			{
				return enableEpisodeRenaming;
			}

			set
			{
				enableEpisodeRenaming = value;
				RaisePropertyChanged("EnableEpisodeRenaming");
			}
		}

		public bool RemoveAccentsFromCharacters
		{
			get
			{
				return removeAccentsFromCharacters;
			}

			set
			{
				removeAccentsFromCharacters = value;
				RaisePropertyChanged("RemoveAccentsFromCharacters");
			}
		}

		public string PatternMovies
		{
			get
			{
				return patternMovies;
			}

			set
			{
				patternMovies = value;
				RaisePropertyChanged("PatternMovies");
			}
		}

		public string PatternSeries
		{
			get
			{
				return patternSeries;
			}

			set
			{
				patternSeries = value;
				RaisePropertyChanged("PatternSeries");
			}
		}

		public string PatternSeason
		{
			get
			{
				return patternSeason;
			}

			set
			{
				patternSeason = value;
				RaisePropertyChanged("PatternSeason");
			}
		}

		public string PatternEpisodes
		{
			get
			{
				return patternEpisodes;
			}

			set
			{
				patternEpisodes = value;
				RaisePropertyChanged("PatternEpisodes");
			}
		}
		#endregion

		#region TabItem : Local

		private ObservableCollection<AddInPropertyChecked> addinReaderPropertiesChecked = new ObservableCollection<AddInPropertyChecked>();
		
		public ObservableCollection<AddInPropertyChecked> AddInReaderProperties
		{
			get
			{
				return addinReaderPropertiesChecked;
			}
			
			set
			{
				addinReaderPropertiesChecked = value;
				RaisePropertyChanged("AddInReaderProperties");
			}
		}
		
		public ICommand UpLocalCommand
		{
			get
			{
				return new RelayCommand(() =>
				                        {
				                        	ReaderAddInSelected.Index--;
				                        	ReaderAddIns[LocalPluginSelectedIndex - 1].Index++;
				                        	ReaderAddIns = new ObservableCollection<AddInInformation>(from a in AddInReaderManager.Current.AddIns
				                        	                                                          orderby a.Index
				                        	                                                          select a);
				                        },
				                        () =>
				                        {
				                        	if (ReaderAddInSelected != null)
				                        	{
				                        		return LocalPluginSelectedIndex != 0;
				                        	}
				                        	else
				                        	{
				                        		return false;
				                        	}
				                        });
			}
		}

		public ICommand DownLocalCommand
		{
			get
			{
				return new RelayCommand(() =>
				                        {
				                        	ReaderAddInSelected.Index++;
				                        	ReaderAddIns[LocalPluginSelectedIndex + 1].Index--;
				                        	ReaderAddIns = new ObservableCollection<AddInInformation>(from a in AddInReaderManager.Current.AddIns
				                        	                                                          orderby a.Index
				                        	                                                          select a);
				                        },
				                        () =>
				                        {
				                        	if (ReaderAddInSelected != null)
				                        	{
				                        		return LocalPluginSelectedIndex != ReaderAddIns.Count - 1;
				                        	}
				                        	else
				                        	{
				                        		return false;
				                        	}
				                        });
			}
		}

		public ICommand OptionLocalCommand
		{
			get
			{
				return new RelayCommand(() =>
				                        {
				                        	AddInReaderManager.Current.ShowOption(ReaderAddInSelected);
				                        },
				                        () =>
				                        {
				                        	return ReaderAddInSelected != null;
				                        });
			}
		}

		public ObservableCollection<AddInInformation> ReaderAddIns
		{
			get
			{
				return readerAddIns;
			}

			set
			{
				readerAddIns = value;
				RaisePropertyChanged("ReaderAddIns");
			}
		}

		public AddInInformation ReaderAddInSelected
		{
			get
			{
				return readerAddInSelected;
			}

			set
			{
				if(readerAddInSelected != null)
				{
					var listTemp = new List<AddInProperty>();
					
					foreach (var element in AddInReaderProperties) {
						if(element.IsChecked)
						{
							listTemp.Add(element.Name);
						}
					}
					
					readerAddInSelected.RetrieveProperty = listTemp;
				}
				
				readerAddInSelected = value;
				
				AddInReaderProperties = new ObservableCollection<AddInPropertyChecked>();
				
				foreach (var addinpropertyAvailable in Enum.GetNames(typeof(AddInProperty))) {
					AddInPropertyChecked propChecked = new AddInPropertyChecked();
					propChecked.Name = (AddInProperty)Enum.Parse(typeof(AddInProperty),addinpropertyAvailable);
					
					if(readerAddInSelected.RetrieveProperty.Contains((AddInProperty)Enum.Parse(typeof(AddInProperty),addinpropertyAvailable)))
					{
						propChecked.IsChecked = true;
					}
					
					AddInReaderProperties.Add(propChecked);
				}
				
				RaisePropertyChanged("LocalOptionImage");
				RaisePropertyChanged("ReaderAddInSelected");
			}
		}
		
		public int LocalPluginSelectedIndex
		{
			get
			{
				return localPluginSelectedIndex;
			}

			set
			{
				localPluginSelectedIndex = value;
				RaisePropertyChanged("LocalPluginSelectedIndex");
			}
		}

		public string LocalOptionImage
		{
			get
			{
				if (ReaderAddInSelected != null)
				{
					return "Images/Config.png";
				}
				else
				{
					return "Images/ConfigDisabled.png";
				}
			}
		}

		#endregion

		#region TabItem : Provider

		private ObservableCollection<AddInPropertyChecked> addinProviderPropertiesChecked = new ObservableCollection<AddInPropertyChecked>();
		
		public ObservableCollection<AddInPropertyChecked> AddInProviderProperties
		{
			get
			{
				return addinProviderPropertiesChecked;
			}
			
			set
			{
				addinProviderPropertiesChecked = value;
				RaisePropertyChanged("AddInProviderProperties");
			}
		}
		
		public ICommand UpProviderCommand
		{
			get
			{
				return new RelayCommand(() =>
				                        {
				                        	ProviderAddInSelected.Index--;
				                        	ProviderAddIns[ProviderPluginSelectedIndex - 1].Index++;
				                        	ProviderAddIns = new ObservableCollection<AddInInformation>(from a in AddInProviderManager.Current.AddIns
				                        	                                                            orderby a.Index
				                        	                                                            select a);
				                        },
				                        () =>
				                        {
				                        	if (ProviderAddInSelected != null)
				                        	{
				                        		return ProviderPluginSelectedIndex != 0;
				                        	}
				                        	else
				                        	{
				                        		return false;
				                        	}
				                        });
			}
		}

		public ICommand DownProviderCommand
		{
			get
			{
				return new RelayCommand(() =>
				                        {
				                        	ProviderAddInSelected.Index++;
				                        	ProviderAddIns[ProviderPluginSelectedIndex + 1].Index--;
				                        	ProviderAddIns = new ObservableCollection<AddInInformation>(from a in AddInProviderManager.Current.AddIns
				                        	                                                            orderby a.Index
				                        	                                                            select a);
				                        },
				                        () =>
				                        {
				                        	if (ProviderAddInSelected != null)
				                        	{
				                        		return ProviderPluginSelectedIndex != ProviderAddIns.Count - 1;
				                        	}
				                        	else
				                        	{
				                        		return false;
				                        	}
				                        });
			}
		}

		public ICommand OptionProviderCommand
		{
			get
			{
				return new RelayCommand(() =>
				                        {
				                        	AddInProviderManager.Current.ShowOption(ProviderAddInSelected);
				                        },
				                        () =>
				                        {
				                        	return ProviderAddInSelected != null;
				                        });
			}
		}

		public ObservableCollection<AddInInformation> ProviderAddIns
		{
			get
			{
				return providerAddIns;
			}

			set
			{
				providerAddIns = value;
				RaisePropertyChanged("ProviderAddIns");
			}
		}

		public AddInInformation ProviderAddInSelected
		{
			get
			{
				return providerAddInSelected;
			}

			set
			{
				if(providerAddInSelected != null)
				{
					var listTemp = new List<AddInProperty>();
					
					foreach (var element in AddInProviderProperties) {
						if(element.IsChecked)
						{
							listTemp.Add(element.Name);
						}
					}
					
					providerAddInSelected.RetrieveProperty = listTemp;
				}
				
				providerAddInSelected = value;
				
				AddInProviderProperties = new ObservableCollection<AddInPropertyChecked>();
				
				foreach (var addinpropertyAvailable in Enum.GetNames(typeof(AddInProperty))) {
					AddInPropertyChecked propChecked = new AddInPropertyChecked();
					propChecked.Name = (AddInProperty)Enum.Parse(typeof(AddInProperty),addinpropertyAvailable);
					
					if(providerAddInSelected.RetrieveProperty.Contains((AddInProperty)Enum.Parse(typeof(AddInProperty),addinpropertyAvailable)))
					{
						propChecked.IsChecked = true;
					}
					
					AddInProviderProperties.Add(propChecked);
				}
				
				RaisePropertyChanged("ProviderOptionImage");
				RaisePropertyChanged("ProviderAddInSelected");
			}
		}

		public int ProviderPluginSelectedIndex
		{
			get
			{
				return providerPluginSelectedIndex;
			}

			set
			{
				providerPluginSelectedIndex = value;
				RaisePropertyChanged("ProviderPluginSelectedIndex");
			}
		}

		public string ProviderOptionImage
		{
			get
			{
				if (ProviderAddInSelected != null)
				{
					return "Images/Config.png";
				}
				else
				{
					return "Images/ConfigDisabled.png";
				}
			}
		}

		#endregion

		#region TabItem : Saver

		private ObservableCollection<AddInPropertyChecked> addinSaverPropertiesChecked = new ObservableCollection<AddInPropertyChecked>();
		
		public ObservableCollection<AddInPropertyChecked> AddInSaverProperties
		{
			get
			{
				return addinSaverPropertiesChecked;
			}
			
			set
			{
				addinSaverPropertiesChecked = value;
				RaisePropertyChanged("AddInSaverProperties");
			}
		}
		
		public ICommand UpSaverCommand
		{
			get
			{
				return new RelayCommand(() =>
				                        {
				                        	SaverAddInSelected.Index--;
				                        	SaverAddIns[SaverAddInSelectedIndex - 1].Index++;
				                        	SaverAddIns = new ObservableCollection<AddInInformation>(from a in AddInSaverManager.Current.AddIns
				                        	                                                         orderby a.Index
				                        	                                                         select a);
				                        },
				                        () =>
				                        {
				                        	if (saverAddInSelected != null)
				                        	{
				                        		return SaverAddInSelectedIndex != 0;
				                        	}
				                        	else
				                        	{
				                        		return false;
				                        	}
				                        });
			}
		}

		public ICommand DownSaverCommand
		{
			get
			{
				return new RelayCommand(() =>
				                        {
				                        	SaverAddInSelected.Index++;
				                        	SaverAddIns[SaverAddInSelectedIndex + 1].Index--;
				                        	SaverAddIns = new ObservableCollection<AddInInformation>(from a in AddInSaverManager.Current.AddIns
				                        	                                                         orderby a.Index
				                        	                                                         select a);
				                        },
				                        () =>
				                        {
				                        	if (SaverAddInSelected != null)
				                        	{
				                        		return SaverAddInSelectedIndex != saverAddIns.Count - 1;
				                        	}
				                        	else
				                        	{
				                        		return false;
				                        	}
				                        });
			}
		}

		public ICommand OptionSaverCommand
		{
			get
			{
				return new RelayCommand(() =>
				                        {
				                        	AddInSaverManager.Current.ShowOption(SaverAddInSelected);
				                        },
				                        () =>
				                        {
				                        	return SaverAddInSelected != null;
				                        });
			}
		}
		
		public ObservableCollection<AddInInformation> SaverAddIns
		{
			get
			{
				return saverAddIns;
			}
			
			set
			{
				saverAddIns =value;
				RaisePropertyChanged("SaverAddIns");
			}
		}
		
		public AddInInformation SaverAddInSelected
		{
			get
			{
				return saverAddInSelected;
			}
			
			set
			{
				if(saverAddInSelected != null)
				{
					var listTemp = new List<AddInProperty>();
					
					foreach (var element in AddInSaverProperties) {
						if(element.IsChecked)
						{
							listTemp.Add(element.Name);
						}
					}
					
					saverAddInSelected.RetrieveProperty = listTemp;
				}
				
				saverAddInSelected = value;
				
				AddInSaverProperties = new ObservableCollection<AddInPropertyChecked>();
				
				foreach (var addinpropertyAvailable in Enum.GetNames(typeof(AddInProperty))) {
					AddInPropertyChecked propChecked = new AddInPropertyChecked();
					propChecked.Name = (AddInProperty)Enum.Parse(typeof(AddInProperty),addinpropertyAvailable);
					
					if(saverAddInSelected.RetrieveProperty.Contains((AddInProperty)Enum.Parse(typeof(AddInProperty),addinpropertyAvailable)))
					{
						propChecked.IsChecked = true;
					}
					
					AddInSaverProperties.Add(propChecked);
				}
				
				RaisePropertyChanged("SaverAddInSelected");
				RaisePropertyChanged("SaverOptionImage");
			}
		}

		public int SaverAddInSelectedIndex
		{
			get
			{
				return saverPluginSelectedIndex;
			}

			set
			{
				saverPluginSelectedIndex = value;
				RaisePropertyChanged("SaverAddInSelectedIndex");
			}
		}

		public string SaverOptionImage
		{
			get
			{
				if (SaverAddInSelected != null)
				{
					return "Images/Config.png";
				}
				else
				{
					return "Images/ConfigDisabled.png";
				}
			}
		}

		#endregion
		
		#region TabItem : GenresMapping
		
		#endregion
		
		public bool CanRebuild { get; private set; }
		
		public string NameWindow
		{
			get
			{
				return Kernel.Instance.GetString("SettingsMW");
			}
		}

		public ICommand ConfirmCommand
		{
			get
			{
				return new RelayCommand(() =>
				                        {
				                        	Config.Instance.RootFolders = Folders.ToList();
				                        	Config.Instance.EnableTraceLogging = IsActivateDebug;
				                        	Config.Instance.AddMissingData = IsOnlyAddMissingData;
				                        	Config.Instance.FilmExtension = FilmExtension;
				                        	Config.Instance.MinBackdropWidth = MinimumSizeSelected;
				                        	Config.Instance.MaxBdSaved = NumberBackdropChoiceSelected;
				                        	Config.Instance.Background = BackgroundDisplaySelected;
				                        	Config.Instance.FullscreenMode = IsSaveWindowGeometry;
				                        	Config.Instance.UseTreeColor = CanUseColorsInTheThree;
				                        	Config.Instance.RenameEpisodes = EnableEpisodeRenaming;
				                        	Config.Instance.RenameMovies = EnableMoviesRenaming;
				                        	Config.Instance.RenameSeasons = EnableSeasonsRenaming;
				                        	Config.Instance.RenameSeries = EnableSeriesRenaming;
				                        	Config.Instance.RemoveAccents = RemoveAccentsFromCharacters;
				                        	Config.Instance.EpisodePattern = PatternEpisodes;
				                        	Config.Instance.MoviePattern = PatternMovies;
				                        	Config.Instance.SeasonPattern = PatternSeason;
				                        	Config.Instance.SeriesPattern = PatternSeries;
				                        	Config.Instance.useProxy = UseProxy;
				                        	Config.Instance.VlcPath = VlcPath;
				                        	Config.Instance.TraceLevel = SelectedTraceLevel;
				                        	Config.Instance.UseGenreMapping = UseGenreMapping;
				                        	Tracer.TracerInstance.Level = (TracerLevel) Enum.Parse(typeof(TracerLevel), SelectedTraceLevel);				                        	
				                        	
				                        	if(saverAddInSelected != null)
				                        	{
				                        		var listTemp = new List<AddInProperty>();
				                        		
				                        		foreach (var element in AddInSaverProperties) {
				                        			if(element.IsChecked)
				                        			{
				                        				listTemp.Add(element.Name);
				                        			}
				                        		}
				                        		
				                        		saverAddInSelected.RetrieveProperty = listTemp;
				                        	}
				                        	
				                        	AddInSaverManager.Current.SaveAllConfiguration();
				                        	
				                        	if(providerAddInSelected != null)
				                        	{
				                        		var listTemp = new List<AddInProperty>();
				                        		
				                        		foreach (var element in AddInProviderProperties) {
				                        			if(element.IsChecked)
				                        			{
				                        				listTemp.Add(element.Name);
				                        			}
				                        		}
				                        		
				                        		providerAddInSelected.RetrieveProperty = listTemp;
				                        	}
				                        	AddInProviderManager.Current.SaveAllConfiguration();
				                        	
				                        	if(readerAddInSelected != null)
				                        	{
				                        		var listTemp = new List<AddInProperty>();
				                        		
				                        		foreach (var element in AddInReaderProperties) {
				                        			if(element.IsChecked)
				                        			{
				                        				listTemp.Add(element.Name);
				                        			}
				                        		}
				                        		
				                        		readerAddInSelected.RetrieveProperty = listTemp;
				                        	}
				                        	AddInReaderManager.Current.SaveAllConfiguration();				                        					                        
				                        	
				                        	Messenger.Default.Send<CloseMessage>(new CloseMessage { WindowToClose = WindowType.SettingsWindow });
				                        });
			}
		}

		public ICommand CancelCommand
		{
			get
			{
				return new RelayCommand(() =>
				                        {
				                        	AddInSaverManager.Current.LoadAllConfigurationFileAddIn();
				                        	AddInProviderManager.Current.LoadAllConfigurationFileAddIn();
				                        	AddInReaderManager.Current.LoadAllConfigurationFileAddIn();
				                        	
				                        	Messenger.Default.Send<CloseMessage>(new CloseMessage { WindowToClose = WindowType.SettingsWindow });
				                        });
			}
		}

		public Visibility ProgressBarVisibility
		{
			get
			{
				return progressBarVisibility;
			}

			set
			{
				progressBarVisibility = value;
				RaisePropertyChanged("ProgressBarVisibility");
			}
		}
		
		
	}
	
	public class AddInPropertyChecked
	{
		public AddInProperty Name { get; set; }
		
		public bool IsChecked { get; set; }
	}
}
